﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DanMuUI
{
    /// <summary>
    /// DanMuTextControl.xaml 的交互逻辑
    /// </summary>
    public partial class DanMuTextControl : UserControl
    {
        public DanMuTextControl()
        {
            InitializeComponent();
            this.Loaded += UserControl_Loaded;

            var _sb = (Storyboard)this.Resources["Storyboard1"];
            Storyboard.SetTarget(_sb.Children[2], this);

            (_sb.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames[1].KeyTime =
                KeyTime.FromTimeSpan(new TimeSpan(Convert.ToInt64(DanMuSettingValue.MainOverlayEffect1 * TimeSpan.TicksPerSecond)));

            (_sb.Children[1] as DoubleAnimationUsingKeyFrames).KeyFrames[1].KeyTime =
                KeyTime.FromTimeSpan(new TimeSpan(Convert.ToInt64(DanMuSettingValue.MainOverlayEffect1 * TimeSpan.TicksPerSecond)));

            (_sb.Children[1] as DoubleAnimationUsingKeyFrames).KeyFrames[2].KeyTime =
                KeyTime.FromTimeSpan(
                    new TimeSpan(
                        Convert.ToInt64((DanMuSettingValue.MainOverlayEffect2 + DanMuSettingValue.MainOverlayEffect1) * TimeSpan.TicksPerSecond)));

            (_sb.Children[2] as DoubleAnimationUsingKeyFrames).KeyFrames[0].KeyTime =
                KeyTime.FromTimeSpan(
                    new TimeSpan(
                        Convert.ToInt64((DanMuSettingValue.MainOverlayEffect3 + DanMuSettingValue.MainOverlayEffect2 + DanMuSettingValue.MainOverlayEffect1) *
                                        TimeSpan.TicksPerSecond)));
            (_sb.Children[2] as DoubleAnimationUsingKeyFrames).KeyFrames[1].KeyTime =
                KeyTime.FromTimeSpan(
                    new TimeSpan(
                        Convert.ToInt64((DanMuSettingValue.MainOverlayEffect4 + DanMuSettingValue.MainOverlayEffect3 + DanMuSettingValue.MainOverlayEffect2 +
                                         DanMuSettingValue.MainOverlayEffect1) * TimeSpan.TicksPerSecond)));
        }

        public void ChangeHeight()
        {
            TextBox.FontSize = DanMuSettingValue.MainOverlayFontSize;
            TextBox.Measure(new Size(DanMuSettingValue.MainOverlayWidth,int.MaxValue));
            var _sb = (Storyboard)Resources["Storyboard1"];
            var _kf1 = _sb.Children[0] as DoubleAnimationUsingKeyFrames;
            _kf1.KeyFrames[1].Value = TextBox.DesiredSize.Height;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= UserControl_Loaded;
        }
    }
}